<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

class Default_Model_FrameMapper extends At_Model_Mapper{
    protected $dbTable;
    protected $tableName = 'frame';

    /**
     * Save a new frame
     *
     * @param Default_Model_Frame $frame
     * @return void
     */
    public function save( Default_Model_Frame &$frame ){
		$values = array(
            'movie_id' 		=> $frame->movieId,
            'mime_type'     => $frame->mimeType,
			'time_code'		=> $frame->timeCode,
            'width'         => $frame->width,
            'height'        => $frame->height,
            'output'        => $frame->output,
            'image'         => $frame->image,
			'icon'			=> $frame->icon,
            'vignette'      => $frame->vignette,
            'thumbnail'     => $frame->thumbnail,
		);

		$values = array_filter( $values );

		if( null == $frame->id ){
			$values['inserted']  = new Zend_Db_Expr( 'NOW()' );
			$frame->id = $this->getTable()->insert( $values );
		}
		else{
			$this->getTable()->update( $values, array( 'id = ?' => $frame->id ) );
		}
    }

	public function findByMovieAndTime( Default_Model_Frame $frame ){
		$db = $this->getTable()->getAdapter();

		$where = array(
			'time_code'	=> $frame->timeCode,
			'movie_id'	=> $frame->movieId
		);

		$values = $db->fetchRow('
			SELECT *
			FROM frame
			WHERE time_code = :time_code
			AND movie_id = :movie_id
		', $where);

		if( false !== $values && key_exists( 'id', $values ) ){
			$values = (object) $values;
			foreach( $values as $key => $value ){
				$frame->$key = $value;
			}
		}

		return $frame;
	}
}
